package test.my.com.myapplicationtest.grid_view_group;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;

import test.my.com.myapplicationtest.R;
import test.my.com.myapplicationtest.image_container.ImageContainer;
import test.my.com.myapplicationtest.utils.ViewUtil;

/**
 * Author : cooper
 * Time :  2018/5/14 下午3:07
 * Description : 网格view
 */
public class GridViewGroup extends ViewGroup {

    private Context mContext;

    private int mWidth = 100;
    private int mHeight = 100;
    private int mMarginDestance; //view之间的间隔

    private int mChildViewWidth;  //子view的宽度
    private int mHorzontalChildCount = 2; //列数

    public GridViewGroup(Context context) {
        super(context);
    }

    public GridViewGroup(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public GridViewGroup(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.GridViewGroup,defStyleAttr,0);
        mMarginDestance = ta.getDimensionPixelOffset(R.styleable.GridViewGroup_mMarginDestance,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
                getResources().getDisplayMetrics()));
        mHorzontalChildCount = ta.getInt(R.styleable.GridViewGroup_mHorzontalChildCount,2);
        ta.recycle();

        init();
    }

    private void init() {

    }

    public void addChildView(View view) {
        addView(view);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // 获得它的父容器为它设置的测量模式和大小
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        mChildViewWidth = (sizeWidth - mHorzontalChildCount*2*mMarginDestance)/mHorzontalChildCount;

        // 容器总宽度
        int vgWidth = 0;
        // 容器总高度
        int vgHeight = 0;

        // 子view总数
        int viewCount = getChildCount();

        for(int i=0;i<viewCount;i++){
            View child = getChildAt(i);
            measureChild(child, MeasureSpec.makeMeasureSpec(mChildViewWidth,modeWidth), MeasureSpec.makeMeasureSpec(mChildViewWidth,modeWidth));
            GridLayoutParams  glp =  (GridLayoutParams)child.getLayoutParams();
            glp.cLeft = (i % mHorzontalChildCount) * mChildViewWidth+(2*(i % mHorzontalChildCount)+1)*mMarginDestance;
            glp.cTop =  (i / mHorzontalChildCount) * mChildViewWidth+(2*(i / mHorzontalChildCount)+1)*mMarginDestance;

        }
        vgWidth = sizeWidth;

        // 计算高度
        int lineNum = 0;
        if (viewCount % mHorzontalChildCount == 0) {
            lineNum = viewCount/mHorzontalChildCount;
        } else {
            lineNum = viewCount/mHorzontalChildCount + 1;
        }
        vgHeight = lineNum*mChildViewWidth + lineNum*mMarginDestance*2;

        setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth
                : vgWidth, (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight
                : vgHeight);
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        int viewCount = getChildCount();
        for(int j=0;j<viewCount;j++){
            View child = getChildAt(j);
            GridLayoutParams clp = (GridLayoutParams) child.getLayoutParams();
            child.layout(clp.cLeft, clp.cTop, clp.cLeft + mChildViewWidth,
                    clp.cTop + mChildViewWidth);
        }
    }

    @Override
    public android.view.ViewGroup.LayoutParams generateLayoutParams(
            AttributeSet attrs) {
        return new GridLayoutParams(getContext(), attrs);
    }

    @Override
    protected android.view.ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return new GridLayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected android.view.ViewGroup.LayoutParams generateLayoutParams(
            android.view.ViewGroup.LayoutParams p) {
        return new GridLayoutParams(p);
    }

    @Override
    protected boolean checkLayoutParams(android.view.ViewGroup.LayoutParams p) {
        return p instanceof GridLayoutParams;
    }


    public static class GridLayoutParams extends ViewGroup.LayoutParams{

        public int cLeft =0;
        public int cTop =0;

        public GridLayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
        }

        public GridLayoutParams(int width, int height) {
            super(width, height);
        }

        public GridLayoutParams(LayoutParams source) {
            super(source);
        }
    }

}
